package com.study.filter;

import javax.servlet.http.HttpServletRequest;

import org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

@Component
public class PreZuulFilterTryCatch extends ZuulFilter{

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() throws ZuulException {
		RequestContext context = RequestContext.getCurrentContext();
		HttpServletRequest request = context.getRequest();
		String host = request.getRemoteHost();
		
		System.out.println("**************PreZuulFilter-host**************** : " + host);
		
		//异常处理：try-catch处理
		try {
			doSomething();
		} catch (Exception e) {
			System.out.println("***********************:" + e.toString());
			context.setThrowable(e);
			context.set("sendErrorFilter.ran", true);
		}
		
		return null;
	}

	private void doSomething() {
		throw new RuntimeException("*******************************:  RuntimeException");
		
	}

	@Override
	public String filterType() {
		return "pre";
	}

	/**
	 * 越小越先执行
	 */
	@Override
	public int filterOrder() {
		return 1;
	}

}
